package com.hdoit.ares.logical.tp.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.tp.dao.IZhonganCallbackDao;
import com.hdoit.ares.logical.tp.model.ZhonganCallback;
import com.hdoit.ares.logical.tp.service.IZhonganCallbackService;
import com.hdoit.ares.logical.viewmodel.tp.ZhonganCallbackSearch;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.RC4Util;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.ZzbConfiguration;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.ZzbManageContainer;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.log.dialect.log4j2.Log4j2LogFactory;

@Component
public class ZhonganCallbackServiceImpl extends SuperService<ZhonganCallback> implements IZhonganCallbackService {
	private IZhonganCallbackDao zhonganCallbackDao;

	public IZhonganCallbackDao getZhonganCallbackDao() {
		return this.zhonganCallbackDao;
	}

	@Resource
	public void setZhonganCallbackDao(IZhonganCallbackDao zhonganCallbackDao) {
		this.zhonganCallbackDao = zhonganCallbackDao;
	}

	public ZhonganCallback resolveZhonganCallback(String result) {
		if (StringUtils.isEmpty(result)) {
			Log4j2LogFactory.get().error("result is null", new Object[0]);
		}
		ZhonganCallback zhonganCallback = new ZhonganCallback();
		Map<String, Object> dataCallbackMap = JSONUtil.parseObj(result);

		zhonganCallback.setId(CommonUtils.getKeyId());
		zhonganCallback.setRequestDate(DateUtils.getCurrent());
		zhonganCallback.setModelStatus(ModelStatus.add);

		zhonganCallback.setNotifyType(MapUtils.getString(dataCallbackMap, "notifyType"));
		String oldPolicyNo = MapUtils.getString(dataCallbackMap, "oldPolicyNo");
		if (!StringUtils.isEmpty(oldPolicyNo)) {
			zhonganCallback.setOldPolicyNo(oldPolicyNo);
		}
		zhonganCallback.setPolicyNo(MapUtils.getString(dataCallbackMap, "policyNo"));

		String sumInsured = MapUtils.getString(dataCallbackMap, "sumInsured");
		if (StringUtils.isEmpty(sumInsured)) {
			zhonganCallback.setSumInsured(sumInsured);
		}
		zhonganCallback.setPremium(MapUtils.getString(dataCallbackMap, "premium"));
		zhonganCallback.setEffectiveDate(MapUtils.getString(dataCallbackMap, "effectiveDate"));
		zhonganCallback.setExpiryDate(MapUtils.getString(dataCallbackMap, "expiryDate"));
		zhonganCallback.setProductName(MapUtils.getString(dataCallbackMap, "productName"));
		zhonganCallback.setPromoteCode(MapUtils.getString(dataCallbackMap, "promoteCode"));
		zhonganCallback.setPromoteName(MapUtils.getString(dataCallbackMap, "promoteName"));
		zhonganCallback.setPromoteFee(MapUtils.getString(dataCallbackMap, "promoteFee"));
		String vehicleType = MapUtils.getString(dataCallbackMap, "vehicleType");
		if (!StringUtils.isEmpty(vehicleType)) {
			zhonganCallback.setVehicleType(vehicleType);
		}
		String openKey = ZzbConfiguration.getInstance().getValue(ZzbManageContainer.zhongan_openKey);
		String sign = MapUtils.getString(dataCallbackMap, "bizContent");
		Log4j2LogFactory.get().error("sign" + sign);
		
		try {
			String bizContentUTF8 = new String(RC4Util.decryRC4(sign, openKey).getBytes("GBK"),"UTF-8") ;
			Log4j2LogFactory.get().error("bizContentUTF8"+bizContentUTF8);
			zhonganCallback.setBizContent(bizContentUTF8);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return zhonganCallback;
	}

	public List<ZhonganCallback> queryList(ZhonganCallbackSearch search, PagerModel pagerModel) {
		return this.zhonganCallbackDao.queryList(search, pagerModel);
	}
}
